This topic demonstrates the steps involved in creating an Interop UserControl in Visual Studio .NET and then using it in a Visual Basic 6.0 project.
This topic requires the Interop Toolkit 2.1, Visual Basic 6.0 and a full version of Visual Studio .NET (Note: Visual Studio Add Ins are not supported in Visual Basic Express Edition).
The first step is to create a project for the Interop UserControl in Visual Studio .NET.
To create a project in Visual Studio .NET
- On the File menu, click New Project.
-
In the New Project dialog box, on the Templates pane under My Templates click VB6 Interop UserControl.
-
In the Name box, type
NamesControl
and then click OK.A new VB6 Interop UserControl template is added to the project, and the UserControl Designer opens.
-
On the File menu, click Save All.
-
In the Save Project dialog box, type
NamesControl
and then click Save.
The next step is to design the appearance of the Interop UserControl by adding controls. For this example, you will add a Label control to display a full name, and three TextBox controls for entering first, last, and middle names.
To add controls to the Interop UserControl
-
From the Toolbox, drag a Label control onto the designer.
-
In the Properties window, change the Name property to
FullName
. -
From the Toolbox, drag three TextBox controls onto the designer. You can arrange them however you like.
-
In the Properties window, change their Name properties to
FirstName
,MiddleName
, andLastName
. -
On the File menu, choose Save All to save your work.
Like any other UserControl, the Interop UserControl has private properties and methods
that define its behavior, and public properties that are exposed when the control
is used in Visual Basic 6.0. The next step is to add a private method that will
update the FullName
label with the contents of the
FirstName
, MiddleName
, and LastName
boxes
as you type, using the TextChanged event handler.
To add private code to the control
- In the Solution Explorer, select InteropUserControl.vb, and then on the View menu choose Code.
-
In the Code Editor, add the following code for the FirstName_TextChanged event handler at the bottom of the class.
Private Sub FirstName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FirstName.TextChanged, MiddleName.TextChanged, LastName.TextChanged
' Display the contents of the three text boxes in the label.
FullName.Text = FirstName.Text & " " & MiddleName.Text & " " & LastName.Text
End Sub
Press F5 to run the program. The UserControl TestContainer opens and displays your UserControl.
Enter your first, middle and last names in the three text boxes—as you type, your
name is displayed in the FullName
label.
The next step is to add code that will expose the FirstName
,
MiddleName
, and LastName
values as public properties in
Visual Basic 6.0.
To add public properties
-
In the Code Editor, add the following code to expose the
FirstName
,MiddleName
, andLastName
values as properties.Public Property FirstNameText() As String Get Return FirstName.Text End Get Set(ByVal value As String) FirstName.Text = value End Set End Property Public Property MiddleNameText() As String Get Return MiddleName.Text End Get Set(ByVal value As String) MiddleName.Text = value End Set End Property Public Property LastNameText() As String Get Return LastName.Text End Get Set(ByVal value As String) LastName.Text = value End Set End Property
Add the following code to expose the value of the FullName
label as a read-only property.
Public ReadOnly Property FullNameText() As String
Get
Return FullName.Text
End Get
End Property
Press F5 to run the program.
In the UserControl TestContainer, scroll to the bottom of the Properties grid and select the FirstNameText property. Enter your name, and then select the FullNameText property—the FirstName text box should display your name, and the FullNameText property should match.
At this point the Interop UserControl is complete and tested in .NET — the next step is to create a project in Visual Basic 6.0 to consume the control.
To create a project in Visual Basic 6.0
-
On the File menu, click New Project.
-
In the New Project dialog box, click Standard EXE and then click OK.
-
On the File menu, click Save Project.
-
In the Save File As dialog box, type
InteropControlTest.frm
and then click Save. -
In the Save Project As dialog box, type
InteropControlTest.vbp
and then click Save.
In the next step you will add the Interop UserControl to the Visual Basic 6.0 Toolbox.
To add the Interop UserControl to the toolbox
-
On the Project menu, click Components.
-
In the Components dialog box, select the Controls tab.
-
Scroll down to the NamesUserControl and select it. Note that the Location is shown a "mscoree.dll" — this is the file that the .NET Framework uses to load Interop UserControls.
-
Check the box next to NamesUserControl and then click OK. The control should appear at the bottom of the Toolbox.
The control is now ready to use as you would any other control. In the final step you will add the control to the form, code a Validate event handler, and test the control.
To add the Interop UserControl and add Visual Basic 6.0 code
-
In the Toolbox, select the InteropUserControl and drag it onto the form. Resize it so that the textboxes are visible.
-
Add a CommandButton control to the form.
-
Double-click the InteropUserControl to open the Code Editor, and in the Events list select Validate.
-
In the Validate event handler add the following code:
Private Sub InteropUserControl1_Validate(Cancel As Boolean)
If InteropUserControl1.FirstNameText = "" Or _
InteropUserControl1.MiddleNameText = "" Or _
InteropUserControl1.LastNameText = "" Then
MsgBox ("Please enter the complete name.")
Cancel = True
End If
End Sub -
Press F5 to run the program. Enter your first and last name, leaving the middle name blank, and then press the Tab key. The message box should display.
Note If your Interop UserControl exposes custom events, there is an additional step that you must take to enable the events. For more information, see How To: Handle Interop UserControl Events.